/* Hawk - An OSGI Based Test Framework
 *
 * Copyright (C) 2010 http://code.google.com/p/hawk-framework/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.hawk.core.model;

import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class BundleGui {
	
	// Attributes
	private String name;
	private Map<String, String> parameters;
	
	private Document document;
	
	/**
	 * Class Constructor
	 */
	public BundleGui(){
		parameters = new LinkedHashMap<String, String>();	
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Map<String, String> getParameters(){
		return parameters;
	}
	
	public void setParameter(String label, String description){
		parameters.put(label, description);
	}
	
	public Set<String> getLabels(){
		return parameters.keySet();
	}

	public String getDescription(String label){
		return parameters.get(label);
	}
	
	public void generate(String xml) throws ParserConfigurationException, SAXException, IOException {
		
		if (xml != null) {
			
			InputSource inputSource  = new InputSource(new StringReader(xml));
		    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		    factory.setNamespaceAware(true);
		    DocumentBuilder builder = factory.newDocumentBuilder();
		    document = builder.parse(inputSource);
		    
			NodeList guiNodes = document.getElementsByTagName("bundle-gui");
			for (int i = 0; i < guiNodes.getLength(); i++) {
				
				Node guiNode = guiNodes.item(i);
				NamedNodeMap attributes = guiNode.getAttributes();
				Node nameAttribute = attributes.getNamedItem("name");
				
				setName(nameAttribute.getNodeValue());
				
				if (guiNode.hasChildNodes()){
					
					NodeList nodeList = guiNode.getChildNodes();
					Node inputNode = nodeList.item(0);
					
					if (inputNode.hasChildNodes()){
						
						NodeList parameterList = inputNode.getChildNodes();
						
						for (int j = 0; j < parameterList.getLength(); j++) {
							Node parameterNode = parameterList.item(j);
							
							NamedNodeMap parameterAttributes = parameterNode.getAttributes();
							
							Node attibuteLabel = parameterAttributes.getNamedItem("label");
							String strLabel = attibuteLabel.getNodeValue();
							
							Node attibuteDescription = parameterAttributes.getNamedItem("description");
							String strDescription = attibuteDescription.getNodeValue();
							
							setParameter(strLabel, strDescription);

						}
					}				
				}
			}
		}
	}
	
}
